const app = getApp();
const pageOptions = {
  // 页面数据
  data: {
    active: 0,
    changeManager: false,
    managerMobile: "",
    managerIdCard: "",
    infoData: [],

    page: 1,
    total_page: 0,
    isLoadMore: true
  },
  // 页面载入时
  onLoad(e) {
    this.init(e);
  },
  // 页面初始化
  init(e) {
    this.getData();
  },
  // 页面准备好时
  onReady() {},
  // 页面显示时
  onShow() {},
  // 页面隐藏时
  onHide() {},
  // 页面卸载时
  onUnload() {},
  // 下拉页面时
  onPullDownRefresh() {},
  // 到达页面底部时
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },
  // 页面滚动时
  onPageScroll() {},
  // 分享时，注：onShareAppMessage不能为async异步函数，会导致不能及时取得返回值，使得分享设置无效
  onShareAppMessage() {},
  toLink(e) {
    let {
      key
    } = e.currentTarget.dataset;
    if (key == 'integral_list') {
      key = "/mircoApp/idShop/integral_list/index?type="+e.currentTarget.dataset.params
    }
    wx.navigateTo({
      url: key,
    })
  },
  to_subordinate(){
    wx.navigateTo({
      url: '/mircoApp/idShop/subordinate/index',
    })
  },
  TabChange(e) {
    this.setData({
      active: e.detail.index
    })
    this.getRecord();
  },
  onClickLeft() {
    this.setData({
      managerMobile: "",
      managerIdCard: "",
      changeManager: false
    })
  },
  changeManagerOpen() {
    this.setData({
      managerMobile: "",
      managerIdCard: "",
      changeManager: true
    })
  },
  to_staffManagement() {
    wx.navigateTo({
      url: '/mircoApp/idShop/staffManagement/index',
    })
  },
  to_Sub_account_buy() {
    wx.navigateTo({
      url: '/mircoApp/idShop/Sub_account_buy/index',
    })
  },
  Sub_account_go() {
    wx.navigateTo({
      url: '/mircoApp/idShop/Sub_account/index',
    })
  },
  toUpdateLeave() {
    wx.navigateTo({
      url: '/mircoApp/idShop/idShop_apply/idShop_apply?tabActive=1',
    })
  },
  async getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    }
    this.setData({
      page: this.data.page + 1
    })
    let urlstr = "";
    if (this.data.infoData.type == 1) {
      urlstr = "plugin.project-party.frontend.controllers.index.bonusLog";
    } else {
      if (this.data.active == 1) {
        urlstr = "plugin.project-party.frontend.controllers.index.performance";
      } else {
        urlstr = "plugin.project-party.frontend.controllers.index.bonusLog";
      }
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(urlstr),
      data: {
        page: this.data.page
      }
    })
    this.data.isLoadMore = true;
    let res = data.data;
    if (data.result) {
      var nextPageData = res.list?.data;
      this.data.listRecord = this.data.listRecord.concat(nextPageData)
      this.setData({
        listRecord: this.data.listRecord
      })
    } else {
      this.data.page = this.data.page - 1;
      this.data.isLoadMore = false;
    }
  },
  async getRecord() {
    let urlstr = "";
    if (this.data.infoData.type == 1) {
      // 达人只有一个分红列表
      urlstr = "plugin.project-party.frontend.controllers.index.bonusLog";
    } else {
      if (this.data.active == 1) {
        urlstr = "plugin.project-party.frontend.controllers.index.performance";
      } else {
        urlstr = "plugin.project-party.frontend.controllers.index.bonusLog";
      }
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(urlstr),
      data: {
        page: this.data.page
      }
    })
    let res = data.data;
    if (data.result) {
      this.setData({
        listRecord: res.list?.data || [],
        total_page: res.list?.pages || 0,
        isLoadMore: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async onClickRight() {
    let json = {
      tel: this.data.managerMobile,
      id_no: this.data.managerIdCard
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.index.changeManager"),
      data: json,
    })
    if (data.result) {
      this.getData();
      this.setData({
        changeManager: false
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.index.index")
    })
    if (data.result) {
      if (data.man_type == 0) {
        wx.showModal({
          title: '提示',
          content: '点击确认跳转申请页面',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: '/mircoApp/idShop/idShop_apply/idShop_apply',
              })
            }
          }
        })
        return
      }
      this.setData({
        infoData: data.data
      })
      this.getRecord();
    } else {
      app.tips(data.msg)
    }
    console.log(data.data)
  },
};

Page(pageOptions);